package main.java.com.yc.navy.snacknet.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yc.navy.snacknet.bean.ProductInfo;
import com.yc.navy.snacknet.bean.TypeInfo;
import com.yc.navy.snacknet.enums.ResultEnum;
import com.yc.navy.snacknet.service.IProductinfoService;
import com.yc.navy.snacknet.util.ConstantIfo;
import com.yc.navy.snacknet.vo.ResultVO;


@RestController
@RequestMapping("/pd")
public class ProductInfoController {
	@Autowired
	private IProductinfoService productinfoService;
	
	//查询所有的商品
	@PostMapping("/findByFirst")
	public ResultVO findByFirst(@RequestParam Map<String, Object> map) {
		Map<String, Object> result = productinfoService.findByFirst(map);
		if(result == null) {
			return new ResultVO(ResultEnum.DATA_NULL);
		}
		return new ResultVO(ResultEnum.SUCCESS, result);
	}
	
	@PostMapping("/finds")
	public ResultVO finds(@RequestParam Map<String, Object> map) {
		List<ProductInfo> list = productinfoService.finds(map);
		if(list == null || list.isEmpty()) {
			return new ResultVO(ResultEnum.DATA_NULL);
		}
		return new ResultVO(ResultEnum.SUCCESS, list);
	}
	
	@PostMapping("/upload")
	public Map<String, Object> upload(MultipartFile upload) {
		Map<String, Object> result = new HashMap<String, Object>();
		String savepath = "";
		File dest = null;
		if(upload != null && upload.getSize() > 0) {
			try {
				savepath = ConstantIfo.UPLOADPATH + "/" + System.currentTimeMillis() + "_" + upload.getOriginalFilename();
				dest = new File(ConstantIfo.SERVERPATH, savepath);
				//报存到指定文件
				upload.transferTo(dest);
				result.put("fileName", upload.getOriginalFilename());
				result.put("uploaded", 1);
				result.put("url", "../../../" + savepath);
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	@PostMapping("/add")
	public ResultVO add(ProductInfo pf, MultipartFile[] photos) {
		String savepath = "";
		File dest = null;
		
		if(photos != null && photos[0].getSize() > 0 && photos.length > 0) {
			String pics = "";
			try {
				for(MultipartFile photo : photos) {
					savepath = ConstantIfo.UPLOADPATH + "/" +System.currentTimeMillis() + "_" + photo.getOriginalFilename();
					dest = new File(ConstantIfo.SERVERPATH, savepath);
					//报存到指定文件
					photo.transferTo(dest);
					if("".equals(pics)) {
						pics = savepath;
					}else {
						pics += ";" + savepath;
					}
				}
				pf.setPics(pics);
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		int result = productinfoService.add(pf);
		if(result > 0) {
			return new ResultVO(ResultEnum.SUCCESS);
		}
		
		if(result == -100) {
			return new ResultVO(ResultEnum.CHECK_ERROR);
		}	
		return new ResultVO(ResultEnum.ERROP);
	}
	
	@PostMapping("/findByPno")
	public ResultVO findByPno(String pno) {
		ProductInfo product = productinfoService.findByPno(pno);
		if(product == null) {
			return new ResultVO(ResultEnum.DATA_NULL);
		}
		return new ResultVO(ResultEnum.SUCCESS, product);
	}
}
